import { glob } from 'glob'
import fse from 'fs-extra'

import fsPromises from 'fs/promises'

import config from '../config/index.js'
import dbWeb from '../doubao/doubao-web.js'
import { sleep } from '../../util.js'

import { pdfToReportPrompt } from './prompt.js'

class Service {
  async uplpad() {
    // const data = config.blog
    const data = config.jxqlhbd
    let files = await glob(data.glob)

    console.log('data', data.length)

    for (let i = data.index; i < files.length; i++) {
      // 进度
      console.log(`${i}/${files.length}`, ((i / files.length) * 100).toFixed(2))

      data.index = i
      fse.writeJSONSync('./config.json', config)

      const pdfPath = files[i]
      const textPath = pdfPath.replace('.pdf', '.txt')

      const result = await dbWeb.upload(pdfPath, pdfToReportPrompt)

      fsPromises.writeFile(textPath, result)

      await sleep(30 * 1000)
    }
  }
}

export default new Service()
